様々なカラースケール#

サポートサイトに掲載しない内容なので、コメントは少なめ。ご了承ください。

初期設定#

Hide code cell content
# tag:hide
# warningsモジュールのインポート
import warnings

# データ解析や機械学習のライブラリ使用時の警告を非表示にする目的で警告を無視
# 本書の文脈では、可視化の学習に議論を集中させるために選択した
# ただし、学習以外の場面で、警告を無視する設定は推奨しない
warnings.filterwarnings("ignore")
Hide code cell content
from pathlib import Path

import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from plotly.graph_objects import Figure
Hide code cell content
# tag:hide
# plotlyの描画設定の定義

# plotlyのグラフ描画用レンダラーの定義
# Jupyter Notebook環境のグラフ表示に適切なものを選択
RENDERER = "plotly_mimetype+notebook"
Hide code cell content
OKABE_ITO = [
    "#000000",  # 黒 (Black)
    "#E69F00",  # 橙 (Orange)
    "#56B4E9",  # 薄青 (Sky Blue)
    "#009E73",  # 青緑 (Bluish Green)
    "#F0E442",  # 黄色 (Yellow)
    "#0072B2",  # 青 (Blue)
    "#D55E00",  # 赤紫 (Vermilion)
    "#CC79A7",  # 紫 (Reddish Purple)
]
Hide code cell content
# tag:hide
def show_fig(fig: Figure) -> None:
    """
    所定のレンダラーを用いてplotlyの図を表示
    Jupyter Bookなどの環境での正確な表示を目的とする

    Parameters
    ----------
    fig : Figure
        表示対象のplotly図

    Returns
    -------
    None
    """

    # 図の周囲の余白を設定
    # t: 上余白
    # l: 左余白
    # r: 右余白
    # b: 下余白
    fig.update_layout(margin=dict(t=25, l=25, r=25, b=25))

    # 所定のレンダラーで図を表示
    fig.show(renderer=RENDERER)

シーケンシャルカラー#

Hide code cell source
fig = px.colors.sequential.swatches_continuous()
show_fig(fig)
Hide code cell source
fig = px.colors.sequential.swatches()
show_fig(fig)

ダイバージェントカラー#

Hide code cell source
fig = px.colors.diverging.swatches_continuous()
show_fig(fig)
Hide code cell source
fig = px.colors.diverging.swatches()
show_fig(fig)

カテゴリカルカラー#

Hide code cell source
fig = px.colors.qualitative.swatches()
show_fig(fig)

OKABE_ITOを表示。

Hide code cell content
def plot_color_palette(palette, num_colors=None, graph_height=150):
    """
    指定されたカラーパレットで横向きの積上げ棒グラフを作成し、
    そのグラフオブジェクトを返す関数。

    Args:
        palette (list): カラーパレット(色のHEXコードのリスト)。
        num_colors (int, optional): 表示する色の数。指定されない場合はカラーパレットの要素数を使用。
        graph_height (int, optional): グラフの高さ。デフォルトは150。
        
    Returns:
        go.Figure: 作成されたグラフオブジェクト。
    """
    # 色数が特に指定されない場合は、カラーパレットの要素数を使用
    if num_colors is None:
        num_colors = len(palette)

    # 積上げ棒グラフを作成
    fig = go.Figure()

    # 各色の部分を追加
    for color in palette[:num_colors]:  # 色数に応じてカラーパレットから色を選択
        fig.add_trace(go.Bar(
            y=['Color Palette'],  # Y軸のラベル
            x=[1],  # 各セグメントの幅
            orientation='h',  # 横向き('h')に設定
            marker=dict(color=color)  # バーの色を設定
        ))

    # グラフの設定
    fig.update_layout(
        barmode='stack',  # 積上げモードに設定
        xaxis=dict(showgrid=False, showticklabels=False, zeroline=False),  # X軸の設定を非表示
        yaxis=dict(showgrid=False, showticklabels=False, zeroline=False),  # Y軸の設定を非表示
        showlegend=False,  # 凡例を非表示に設定
        height=graph_height,  # 図の高さを設定
    )

    # グラフオブジェクトを返す
    return fig
Hide code cell source
fig = plot_color_palette(OKABE_ITO)
show_fig(fig)
Hide code cell source
fig = plot_color_palette(px.colors.diverging.Portland)
show_fig(fig)
Hide code cell content
import plotly.io as pio
active_template = pio.templates.default
print(pio.templates[active_template].layout.colorscale.sequential)
((0.0, '#0d0887'), (0.1111111111111111, '#46039f'), (0.2222222222222222, '#7201a8'), (0.3333333333333333, '#9c179e'), (0.4444444444444444, '#bd3786'), (0.5555555555555556, '#d8576b'), (0.6666666666666666, '#ed7953'), (0.7777777777777778, '#fb9f3a'), (0.8888888888888888, '#fdca26'), (1.0, '#f0f921'))